<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript">
        window._AMapSecurityConfig = {
            securityJsCode: 'e1b93e9730d1a02819c9d41e30a47da5',
        }
    </script>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=97e1969986cfdad21dd89813a23d5a28">
    </script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body,
        #container {
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>
    <button onclick="handleShowTraffic()">实时路况</button>
    <div id="container"></div>

    <script>
        let map = new AMap.Map('container', {
            // 中心
            center: [112.54, 37.85],
            // 缩放 (3, 20)
            zoom: 15,
            viewMode: '3D',
            pitch: 45
        })
        var traffic = new AMap.TileLayer.Traffic({
            'autoRefresh': true, //是否自动刷新，默认为false
            'interval': 180, //刷新间隔，默认180s
        });

        
        let isTraffic = false
        function handleShowTraffic() {
            console.log(66);
            if (!isTraffic) {
                map.add(traffic); //通过add方法添加图层
                isTraffic = true
            } else {
                map.remove(traffic)
                isTraffic = false
            }
        }
    </script>
</body>

</html>